package com.cxhome.cxinfox.cxdepartment;

import java.util.List;

import com.jfinal.plugin.activerecord.Page;
import com.traffic.managent.annotation.ControllerBind;
import com.traffic.managent.base.CxBaseController;
import com.traffic.managent.model.system.Department;
import com.traffic.managent.util.StringUtils;

/**
 * 部门
 * 
 */
@ControllerBind(controllerKey = "/system/cxdepartment")
public class DepartmentController extends CxBaseController {

	private static final String path = "/pages/system/cxdepartment/cxdepartment_";

	public void index() {
		list();
	}

	public void list() {
		Department model = getModelByAttr(Department.class);

		StringBuilder orderSql = new StringBuilder("");
		// 排序
		String orderBy = getBaseForm().getOrderBy();
		orderBy = orderBy == "" ? "ids" : orderBy;
		orderBy = "p." + orderBy;
		if (StringUtils.isEmpty(orderBy)) {
			orderSql.append(" order by t.ids desc ");
		} else {
			orderSql.append(" order by ").append(orderBy);
		}

		Page<Department> page = DepartmentService.service.paginate(model, getPaginator(), orderSql);
		setAttr("page", page);
		setAttr("attr", model);
		List<Department> departments = Department.dao.find("select * from cx_department  ");
		setAttr("cxdepartments", departments);
		render(path + "list.html");
	}

	/**
	 * 跳转新增页面
	 */
	public void add() {
		// 获取页面信息,设置目录传入
		setAttr("selectParentDepartments", new DepartmentService().selectDepart(getPara()));

		render(path + "add.html");
	}

	/**
	 * 查看详情
	 */
	public void view() {
		Department model = Department.dao.findById(getPara());
		setAttr("model", model);
		render(path + "view.html");
	}

	
	/**
	 * 数据删除
	 */
	public void delete() {
		Department.dao.deleteById(getPara());
		list();
	}

	/**
	 * 跳转编辑页面
	 */
	public void edit() {
		Department model = Department.dao.findById(getPara());
		setAttr("model", model);
		setAttr("selectParentDepartments", new DepartmentService().selectDepart(model.getStr("parentdepartmentids")));
		render(path + "edit.html");
	}

	/**
	 * 增加保存
	 */
	public void saveAdd() {
		Department model = getModel(Department.class);

		DepartmentService.service.saveAdd(model);

		renderMessage("保存成功");
	}

	/**
	 * 编辑保存
	 */
	public void saveEdit() {
		Department model = getModel(Department.class);
		DepartmentService.service.saveEdit(model);
		renderMessage("保存成功");
	}

}
